﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.Geometry
{
    /// <summary>
    /// The class represents a plane triangle with three vertices.
    /// </summary>
    [Serializable]
    public class Triangle2D : GeometricObject, ITriangle2D
    {
        /// <summary>
        /// The first point of the triangle.
        /// </summary>
        private Point2D pointA;

        /// <summary>
        /// The second point of the triangle.
        /// </summary>
        private Point2D pointB;

        /// <summary>
        /// The third point of the triangle.
        /// </summary>
        private Point2D pointC;

        /// <summary>
        /// Initializes a new instance of the <see cref="Triangle2D"/> class.
        /// </summary>
        public Triangle2D()
        {
            this.pointA = new Point2D();
            this.pointB = new Point2D();
            this.pointC = new Point2D();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Triangle2D"/> class.
        /// </summary>
        /// <param name="triangle">The triangle to clone.</param>
        public Triangle2D(Triangle2D triangle)
        {
            if (triangle == (Triangle2D) null)
            {
                throw new ArgumentNullException("triangle");
            }

            this.pointA = triangle.PointA;
            this.pointB = triangle.PointB;
            this.pointC = triangle.PointC;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Triangle2D"/> class.
        /// </summary>
        /// <param name="pointA">The first point of the triangle.</param>
        /// <param name="pointB">The second point of the triangle.</param>
        /// <param name="pointC">The third point of the triangle.</param>
        public Triangle2D(Point2D pointA, Point2D pointB, Point2D pointC)
        {
            if (pointA == (Point2D) null)
            {
                throw new ArgumentNullException("pointA");
            }

            if (pointB == (Point2D) null)
            {
                throw new ArgumentNullException("pointB");
            }

            if (pointC == (Point2D) null)
            {
                throw new ArgumentNullException("pointC");
            }

            this.pointA = pointA;
            this.pointB = pointB;
            this.pointC = pointC;
        }

        /// <summary>
        /// Gets or sets the first point of the triangle.
        /// </summary>
        /// <value>The first point of the triangle.</value>
        public Point2D PointA
        {
            get { return pointA; }
            set { pointA = value; }
        }

        /// <summary>
        /// Gets or sets the second point of the triangle.
        /// </summary>
        /// <value>The second point of the triangle.</value>
        public Point2D PointB
        {
            get { return pointB; }
            set { pointB = value; }
        }

        /// <summary>
        /// Gets or sets the third point of the triangle.
        /// </summary>
        /// <value>The third point of the triangle.</value>
        public Point2D PointC
        {
            get { return pointC; }
            set { pointC = value; }
        }

        /// <summary>
        /// Scales the triangle by a specified scale factor.
        /// </summary>
        /// <param name="scaleFactor">The scale factor.</param>
        public void ScaleTriangle(double scaleFactor)
        {
            this.pointA.ScaleTransformation(scaleFactor);
            this.pointB.ScaleTransformation(scaleFactor);
            this.pointC.ScaleTransformation(scaleFactor);
        }

        /// <summary>
        /// Scales the triangle by two specified scale factors. One for the x-axis
        /// and one for the y-axis. 
        /// </summary>
        /// <param name="scaleFactorX">The scale factor for the x axis.</param>
        /// <param name="scaleFactorY">The scale factor for the y axis.</param>
        public void ScaleTriangle(double scaleFactorX, double scaleFactorY)
        {
            this.pointA.ScaleTransformation(scaleFactorX, scaleFactorY);
            this.pointB.ScaleTransformation(scaleFactorX, scaleFactorY);
            this.pointC.ScaleTransformation(scaleFactorX, scaleFactorY);
        }

        /// <summary>
        /// Translates the triangle with a specified translate value.
        /// </summary>
        /// <param name="translateValue">The translate value.</param>
        public void TranslateTriangle(double translateValue)
        {
            this.pointA.TranslateTransformation(translateValue);
            this.pointB.TranslateTransformation(translateValue);
            this.pointC.TranslateTransformation(translateValue);
        }

        /// <summary>
        /// Translates the triangle with two specified translate values.
        /// </summary>
        /// <param name="translateValueX">The translate value for the x-axis.</param>
        /// <param name="translateValueY">The translate value for the y-axis.</param>
        public void TranslateTriangle(double translateValueX, double translateValueY)
        {
            this.pointA.TranslateTransformation(translateValueX, translateValueY);
            this.pointB.TranslateTransformation(translateValueX, translateValueY);
            this.pointC.TranslateTransformation(translateValueX, translateValueY);
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="a">The first triangle.</param>
        /// <param name="b">The second triangle.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(Triangle2D a, Triangle2D b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object) a == null) || ((object) b == null))
            {
                return false;
            }

            return ((a.PointA == b.PointA) && (a.PointB == b.PointB) && (a.PointC == b.PointC));
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="a">The first triangle.</param>
        /// <param name="b">The second triangle.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(Triangle2D a, Triangle2D b)
        {
            return !(a == b);
        }

        /// <summary>
        /// Compares the current instance of a triangle with another triangle object. 
        /// </summary>
        /// <param name="compare">The triangle to compare.</param>
        /// <returns>True if the position of the triangles are even otherwise the method will 
        /// return false.</returns>
        public bool CompareTo(Triangle2D compare)
        {
            return this == compare;
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return this.pointA.ToString() + " | " + this.pointB.ToString() + " | " + this.pointC.ToString();
        }
    }
}